//
//  XVCSomeoneSeenU.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/4/11.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "XVCSomeoneSeenU.h"
#import "XXTool.h"
#import "XTCSomeoneSeenU.h"

@interface XVCSomeoneSeenU ()<UITableViewDataSource,UITableViewDelegate,XTCSomeoneSeenUDelegate>

@property (nonatomic,strong) UITableView *tableview;

@end

@implementation XVCSomeoneSeenU
{
    NSMutableArray <XMLSeenUModel *> *useData;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(void)initParameter
{
    useData = [[NSMutableArray alloc] init];
}

-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"看过你的人", nil)];
}

-(void)initData
{
    [self getData];
}

-(void)refreshLayout:(CGRect)frame
{
    self.tableview.frame = CGRectMake(0, 0, frame.size.width
                                      , frame.size.height);
}

-(void)getData
{
    [XMLSeenUModel requestSeenUPersonListWithCount:0
                                        completion:^(BOOL isSuccess, NSMutableArray<XMLSeenUModel *> *seenUList) {
                                            if (isSuccess)
                                            {
                                                useData = seenUList;
                                                [_tableview reloadData];
                                            }
                                            
                                            [self.view stopLoading];
                                            [_tableview.mj_header endRefreshing];
                                        }];
}

-(void)footerFresh
{
    [XMLSeenUModel requestSeenUPersonListWithCount:useData.count
                                        completion:^(BOOL isSuccess, NSMutableArray<XMLSeenUModel *> *seenUList) {
                                            if (isSuccess)
                                            {
                                                if (seenUList.count==0)
                                                {
                                                    //[SVProgressHUD showInfoWithStatus:NSLocalizedString(@"暂无更多消息", nil)];
                                                }
                                                else
                                                {
                                                    [useData addObjectsFromArray:seenUList];
                                                }
                                                [_tableview reloadData];
                                            }
                                            [_tableview.mj_footer endRefreshing];
                                        }];
}

#pragma mark XTCSomeoneSeenUDelegate
-(void)someoneSeenUCellDeleteSuccessWithCell:(XTCSomeoneSeenU *)cell
{
    NSIndexPath *tempIndexPath= [_tableview indexPathForCell:cell];
    [useData removeObjectAtIndex:tempIndexPath.row];
    [_tableview deleteRowsAtIndexPaths:@[tempIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
    [SVProgressHUD showSuccessWithStatus:NSLocalizedString(@"删除成功", nil)];
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XTCSomeoneSeenU *cell = [XTCSomeoneSeenU cellSelectionStyleNoneWithTableView:tableView];
    [cell setCellWithData:useData[indexPath.row] personType:XVVReleasePersonInfomationSeenU];
    cell.delegate = self;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [XTCSomeoneSeenU getHeight:useData[indexPath.row] personType:XVVReleasePersonInfomationSeenU];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return useData.count;
}

#pragma mark Api
+(void)pushSomeSeenUVCWithSourceVC:(UIViewController *)sourceVC
{
    XVCSomeoneSeenU *vc = [[XVCSomeoneSeenU alloc] init];
    if (sourceVC)
    {
        [sourceVC.navigationController pushViewController:vc animated:YES];
    }
}

@end
